package com.dianwoba.core.solr;

import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.TermsResponse;
import org.apache.solr.client.solrj.response.TermsResponse.Term;

import com.dianwoba.core.exception.SystemException;

/**
 * SLOR处理器
 * @author 堵成杰
 * @since 2.0
 */
public class SolrService {

	
	
	/**
	 * 服务器类型是否为嵌入式
	 */
	private Boolean isEmbed;
	
	/**
	 * 远程服务器URL
	 */
	private String url;
	
	/**
	 * solr过期时间
	 */
	private Integer soTimeout;
	
	/**
	 * 链接过期时间
	 */
	private Integer connectionTimeout;
	
	/**
	 * 单台服务器最大链接数
	 */
	private Integer maxConnectionPerHost;
	
	/**
	 * 最大总连接数
	 */
	private Integer maxTotalConnection;
	
	private Boolean followRedirect;
	private Boolean allowCompression;
	private Integer maxRetries;
	private String solrHome;
	
	/**
	 * SOLR服务器
	 */
	public SolrServer solrServer;
	
	

	/**
	 * 最大查询结果数量
	 */
	protected int defaultLimit = 10000;

	
	
	
	/**
	 * 初始化
	 */
	public void init(){
	
		try{
			
			//远程服务器
			if(!isEmbed){
				HttpSolrServer _solrServer = new HttpSolrServer(url);
				_solrServer.setSoTimeout(soTimeout);
				_solrServer.setConnectionTimeout(connectionTimeout);
				_solrServer.setDefaultMaxConnectionsPerHost(maxConnectionPerHost);
				_solrServer.setMaxTotalConnections(maxTotalConnection);
				_solrServer.setFollowRedirects(followRedirect);
				_solrServer.setAllowCompression(allowCompression);
				_solrServer.setMaxRetries(maxRetries);
				_solrServer.setParser(new XMLResponseParser());
				solrServer = _solrServer;
				
			//嵌入式服务器
			}else{

			}
			
		}catch(Exception e){
			throw new SystemException("[ERR]Solr服务器启动，初始化失败。", e);
		}
	}

	/**
	 * 取得SOLR服务器对象
	 * @return	SOLR服务器对象
	 */
	public SolrServer getSolrServer(){
		return solrServer;
		
	}

	/**
	 * 指定查找结果类型
	 * @param <T>			结果集类型
	 * @param baseClass		结果集类型
	 * @return
	 */
	public <T> SolrSelect<T> from(Class<T> baseClass) {
		return new SolrSelect<T>(this, baseClass);
	}

	
	/**
	 * 插入单条数据
	 * @param <T>
	 * @param entity
	 * @return
	 */
	public <T> boolean insert(T entity) {
		try{
			solrServer.addBean(entity);
		}catch(Exception e){
			e.printStackTrace();
			return false;
			
		}
		return true;
	}

	/**
	 * 插入多条数据
	 * @param <T>
	 * @param entities
	 * @return
	 */
	public  <T> boolean insert(List<T> entities) {
		try{
			solrServer.addBeans(entities);
		}catch(Exception e){
			e.printStackTrace();
			return false;
			
		}
		return true;
	}

	/**
	 * 更新单条数据
	 * @param <T>
	 * @param entity
	 * @return
	 */
	public <T> boolean update(T entity) {
		try{
			solrServer.addBean(entity);
		}catch(Exception e){
			e.printStackTrace();
			return false;
			
		}
		return true;
	}

	
	/**
	 * 更新多条数据
	 * @param <T>
	 * @param entities
	 * @return
	 */
	public  <T> boolean update(List<T> entities) {
		try{
			solrServer.addBean(entities);
		}catch(Exception e){
			e.printStackTrace();
			return false;
			
		}
		return true;
	}
	
	/**
	 * 根据id删除
	 * @param id
	 * @return
	 */
	public boolean delete(String id) {
		try{
		
			solrServer.deleteById(id);
		}catch(Exception e){
			e.printStackTrace();
			return false;
			
		}
		return true;
	}

	/**
	 * 根据id集合删除
	 * @param id
	 * @return
	 */
	public boolean delete(List<String> ids) {
		try{
		
			solrServer.deleteById(ids);
		}catch(Exception e){
			e.printStackTrace();
			return false;
			
		}
		return true;
	}
	
	/**
	 * 根据query删除
	 * @param id
	 * @return
	 */
	public boolean deleteByQuery(String query) {
		try{
		
			solrServer.deleteByQuery(query);
		}catch(Exception e){
			e.printStackTrace();
			return false;
			
		}
		return true;
	}

	/**
	 * 提交操作
	 * @return	操作结果
	 */
	public boolean commit() {
		try {
			
			solrServer.commit(false, false, true);
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}

	/**
	 * 优化操作
	 * @return	操作结果
	 */
	public boolean optimize() {
		try {
			
			solrServer.optimize();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	/**
	 * 查找热门关键字
	 * @param keyWord	关键字
	 * @param limit		返回件数
	 * @return
	 */
	public List<Term> querySuggestWord(String keyWord,int limit){
		List<Term> terms = null;
		try {SolrQuery query = new SolrQuery();
	     query.addTermsField("entityName");
	     query.setTerms(true);
	     query.setTermsLimit(limit);
	     query.setTermsPrefix(keyWord);
	     query.setRequestHandler("/terms");
	     System.out.println(query);
	         QueryResponse response = getSolrServer().query(query);
	         TermsResponse termResp = response.getTermsResponse();
	         terms = termResp.getTerms("entityName");
	     } catch (Throwable e) {
	    	 e.printStackTrace();
	    	 terms = null;
	     }
	     return terms;
	}

	public Boolean getIsEmbed() {
		return isEmbed;
	}

	public void setIsEmbed(Boolean isEmbed) {
		this.isEmbed = isEmbed;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Integer getSoTimeout() {
		return soTimeout;
	}

	public void setSoTimeout(Integer soTimeout) {
		this.soTimeout = soTimeout;
	}

	public Integer getConnectionTimeout() {
		return connectionTimeout;
	}

	public void setConnectionTimeout(Integer connectionTimeout) {
		this.connectionTimeout = connectionTimeout;
	}

	public Integer getMaxConnectionPerHost() {
		return maxConnectionPerHost;
	}

	public void setMaxConnectionPerHost(Integer maxConnectionPerHost) {
		this.maxConnectionPerHost = maxConnectionPerHost;
	}

	public Integer getMaxTotalConnection() {
		return maxTotalConnection;
	}

	public void setMaxTotalConnection(Integer maxTotalConnection) {
		this.maxTotalConnection = maxTotalConnection;
	}

	public Boolean getFollowRedirect() {
		return followRedirect;
	}

	public void setFollowRedirect(Boolean followRedirect) {
		this.followRedirect = followRedirect;
	}

	public Boolean getAllowCompression() {
		return allowCompression;
	}

	public void setAllowCompression(Boolean allowCompression) {
		this.allowCompression = allowCompression;
	}

	public Integer getMaxRetries() {
		return maxRetries;
	}

	public void setMaxRetries(Integer maxRetries) {
		this.maxRetries = maxRetries;
	}

	public String getSolrHome() {
		return solrHome;
	}

	public void setSolrHome(String solrHome) {
		this.solrHome = solrHome;
	}

	public void setSolrServer(SolrServer solrServer) {
		this.solrServer = solrServer;
	}

	public int getDefaultLimit() {
		return defaultLimit;
	}

	public void setDefaultLimit(int defaultLimit) {
		this.defaultLimit = defaultLimit;
	}

	
}
